QuanLyKhoangSan = {
	typeAction:"",
	callFunction:function(coor,appId,maloai,data,type){
		//var coor = "541818.39 1160332.10,542423.19 1160214.50,542704.43 1159959.17,542339.19 1159777.70,541809.46 1159852.56";
		/*var coor = "539365.00 1163047.41;540491.59 1162821.89";
		var appId="QLKS";
		var maloai="1";
		var data="CBKS_01,DKCB_01,demo;CBKS_02,DKCB_02,1";
		var type = "insert";*/
		$.ajax({
		 	type:"POST",
			url:sPortletJSP+"GetData.jsp",
			data:{
				attr:'create_geom',
				coor:coor,
				maloai:maloai,
				dulieu:data,
				appid:appId,
				type:type
			},
			async:false,
			success:function(data){
				var rs = data.trim();
				var array = rs.split(";");
				var arrSuccess = array[0].split("<p>");
				var arrError = array[1].split("<p>");
				var sAlert = LANGUAGE["exec_Success"];
				sAlert = sAlert.replace("{1}",arrSuccess[0]);
				sAlert = sAlert.replace("{succ}",arrSuccess[1]);
				sAlert = sAlert.replace("{2}",arrError[0]);
				sAlert = sAlert.replace("{error}",arrError[1]);
				MapAction.RefreshAction();
				$("#p_Map_alert_info").html(sAlert);
			} 
 		});
	},	
	insertCoor:function(coor,appId,data){
		
	},
	biddingCombobox:function(){
		var combobox = document.getElementById("selectAssetInsertgeom");
		$(combobox).empty();
		var option = document.createElement('option');
		option.text = LANGUAGE["option_Tool_select_0"];
		option.value = 0;
		combobox.appendChild(option);
		for (var i = 0; i < mapWFS.length; i++) {
			if(mapWFS[i].SearchAttr){
				var elOptNew = document.createElement('option');
				elOptNew.text = mapWFS[i].Layer_Name;
				elOptNew.value = mapWFS[i].LayerId;
				combobox.appendChild(elOptNew);				
			}
		}
		$("#selectAssetInsertgeom").uniform();
	},
	showDialog:function(){
		ToolMapAction.biddingCombobox();
		$("#divDialog_InsertGeomFromHoSo").dialog("open");
		$("#divContentDiemCheBienGeom").hide();
		$("#divContentVungKhaiThacGeom").hide();
		$("#divContentVungThamDoGeom").hide();
	},
	selectLayer:function(ctrl){
		var layer = ctrl.value;
		$("#divContentDiemCheBienGeom").hide();
		$("#divContentVungKhaiThacGeom").hide();
		$("#divContentVungThamDoGeom").hide();
		arrInputControl = [];
		if(layer==LayerAssetName.cheBienKhoangSan){
			$("#divContentDiemCheBienGeom").show();
			arrInputControl = ["#txtMaDangKy_CheBien","#txtMaCheBien",
			                   "#txtTrangThai_CheBien"];
			arrInputCoor = ["#txtCoorX_CheBien","#txtCoorY_CheBien"];
			ToolMapAction.typeAction = "#txtTypeAction_CheBien";
			resetDiemCheBien();
		}else if(layer==LayerAssetName.khaiThacKhoangSan){
			$("#divContentVungKhaiThacGeom").show();
			arrInputControl = ["#txtMaDangKy_KhaiThac","#txtMaKhaiThac","#txtTrangThai_KhaiThac"];
			arrInputCoor = [];
			ToolMapAction.typeAction = "#txtTypeAction_KhaiThac";
			resetDiemKhaiThac();
		}else if(layer==LayerAssetName.thamDoKS){
			$("#divContentVungThamDoGeom").show();
			arrInputControl = ["#txtMaDangKy_ThamDo","#txtMaDean","#txtTrangThai_ThamDo"];
			arrInputCoor = [];
			ToolMapAction.typeAction = "#txtTypeAction_ThamDo";
			resetDiemThamDo();
		}
	},  
	acceptData:function(){
		$("#divDisplay_textError").html("");
		var sCoor = arrInputCoor.length;
		if(sCoor==0){
			$("#divDisplay_textError").html(LANGUAGE["input_coor"]);
			return;
		}
		var strCoor = "",data="";
		var i=0,index=0;
		for(i=0;i<sCoor/2;i++){
			var x = $(arrInputCoor[index]).val();
			var y = $(arrInputCoor[index+1]).val();
			index = index+2;
			if(strCoor.length==0)strCoor=x+" "+y;
			else strCoor+=","+x+" "+y;
		}
		for(i=0;i<arrInputControl.length;i++){
			var val = $(arrInputControl[i]).val();
			if(data.length==0)data=val;
			else data+=","+val;
		}
		var appId = "",maloai="";
		var type=$(ToolMapAction.typeAction).val();
		var layer = $("#selectAssetInsertgeom").val();
		$.ajax({
			type : "POST",
			url:sPortletJSP+"GetData.jsp",
			async:false,
			data:{
				attr:"select_asset",
				layer:layer
			},
			success : function(data) {
				var rs = data.trim();
				if(rs!="null"&&rs!=null){
					var arrData = rs.split(",");
					appId = arrData[1];
					maloai = arrData[0];
				}
			}
		});
		this.callFunction(strCoor, appId, maloai, data, type);
		if(layer==LayerAssetName.cheBienKhoangSan){
			resetDiemCheBien();
		}else if(layer==LayerAssetName.khaiThacKhoangSan){
			resetDiemKhaiThac();
		}else if(layer==LayerAssetName.thamDoKS){
			resetDiemThamDo();
		}
	},
	getLayerInsert:function(appId){
		var index = null;
		var layerId=this.getLayerIdByAppId(appId);
		for(var i=0;i<vectorArray.length;i++){
			if(vectorArray[i].LayerId==layerId){
				var s = vectorArray[i].SearchAttr;
				if(s){
					index = vectorArray[i].IndexLayer;
					break;
				}
			}
		}
		return index;
	},
	getLayerIdByAppId:function(id){
		var layer = "";
		switch (id) {
			case "1":
				layer = LayerAssetName.cheBienKhoangSan;
				break;
			case "2":
				layer = LayerAssetName.khaiThacKhoangSan;
				break;
			case "3":
				layer = LayerAssetName.vungCapPhep;
				break;
			case "4":	
				layer = LayerAssetName.thamDoKS;
				break;	
			case "5":
				layer = LayerAssetName.camKhaiThac;
				break;	
			case "6":
				layer = LayerAssetName.vungDauTu;
				break;
			case "7":
				layer = LayerAssetName.vungQuyHoach;
				break;
			case "8":
				layer = LayerAssetName.diemKhoanGieng;
				break;	
			case "9":
				layer = LayerAssetName.diemONhiem;
				break;	
			case "10":
				layer = LayerAssetName.diemNguonThai;
				break;
			case "11":
				layer = LayerAssetName.diemXuLy;
				break;	
			case "12":
				layer = LayerAssetName.diemTapKet;
				break;		
			default:
				break;
		}
		return layer;
	},
	getGeomByLayerId:function(layer){
		var geom = null;
		switch (layer) {
			case LayerAssetName.khaiThacKhoangSan:
				geom = Geometries.Polygon;
				break;
			case LayerAssetName.cheBienKhoangSan:
				geom = Geometries.Point;
				break;
			case LayerAssetName.thamDoKS:
				geom = Geometries.Polygon;
				break;	
			case LayerAssetName.vungCapPhep:
				geom = Geometries.Polygon;
				break;
			case LayerAssetName.camKhaiThac:
				geom = Geometries.Polygon;
				break;
			case LayerAssetName.vungDauTu:
				geom = Geometries.Polygon;
				break;
			case LayerAssetName.vungQuyHoach:
				geom = Geometries.Polygon;
				break;	
			case LayerAssetName.diemCapPhep:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemThamDoNuoc:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemKTNuocMat:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemXaThai:
				geom = Geometries.Point;
				break;	
			case LayerAssetName.diemKhoanGieng:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemONhiem:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemNguonThai:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemXuLy:
				geom = Geometries.Point;
				break;
			case LayerAssetName.diemTapKet:
				geom = Geometries.Point;
				break;	
			default:
				break;
		}
		return geom;
	},
	showHidenLayer:function(appId){
		var layerId = this.getLayerIdByAppId(appId);
		var i=0;
		for(i=0;i<vectorArray.length;i++){
			if(vectorArray[i].SearchAttr){
				if(layerId == base.trim(vectorArray[i].LayerId.toLowerCase())){
					var index = vectorArray[i].IndexLayer;
					map.layers[index].setVisibility(true);
				}else{
					var index = vectorArray[i].IndexLayer;
					map.layers[index].setVisibility(false);					
				}
			}
		}
	},
	getPortletJSP:function(layerId){
		var url = "";
		switch (layerId) {
		case LayerAssetName.cheBienKhoangSan:
			url = "asset/ui-form/diemchebien.jsp";
			break;
		case LayerAssetName.khaiThacKhoangSan:
			url = "asset/ui-form/khaithackhoangsan.jsp";
			break;
		case LayerAssetName.thamDoKS:
			url = "asset/ui-form/thamdo.jsp";
			break;
		case LayerAssetName.diemThamDoNuoc:
			url = "asset/ui-form/thamdonguonnuoc.jsp";
			break;
		case LayerAssetName.diemKTNuocMat:
			url = "asset/ui-form/khaithacnuocmat.jsp";
			break;
		case LayerAssetName.diemXaThai:
			url = "asset/ui-form/diemxathai.jsp";
			break;
		case LayerAssetName.diemKhoanGieng:
			url = "asset/ui-form/diemkhoangieng.jsp";
			break;
		case LayerAssetName.diemNguonThai:
			url = "asset/ui-form/diemnguonthai.jsp";
			break;
		case LayerAssetName.diemONhiem:
			url = "asset/ui-form/diemonhiem.jsp";
			break;
		case LayerAssetName.diemXuLy:
			url = "asset/ui-form/diemxuly.jsp";
			break;
		case LayerAssetName.diemTapKet:
			url = "asset/ui-form/diemtapket.jsp";
			break;
		default:
			break;
		}
		return url;
	},
	getDataFeature:function(feature,layerId,type){
		var data = null;
		switch (layerId) {
		case LayerAssetName.cheBienKhoangSan:
			if(type=="insert"){
				data = {
					layer:layerId,
					action:"insert",
					madangky:"",
					machebien:"",
					coorx:feature.geometry.x,
					coory:feature.geometry.y,
					trangthai:"-1"
				};
			}else{
				data = {
						layer:layerId,
						action:"edit",
						madangky:feature.attributes["ma_dangky"],
						machebien:feature.attributes["ma_chebien"],
						coorx:feature.geometry.x,
						coory:feature.geometry.y,
						trangthai:feature.attributes["trangthai"]
					};
			}
			break;
		case LayerAssetName.khaiThacKhoangSan:
			var sCoor = "";
			var sFirst = "";
			var sVertices = feature.geometry.getVertices();
			for(var j=0;j<sVertices.length;j++){
				var coor =sVertices[j].x+" "+sVertices[j].y;
				if(j==0)sFirst = coor;
				if(sCoor==""&&sCoor.length==0)sCoor = coor;
				else sCoor+=","+coor;
			}
			sCoor+=","+sFirst;
			if(type=="insert"){
				data = {
					layer:layerId,
					action:"insert",
					madangky:"",
					makhaithac:"",
					coor:sCoor,
					trangthai:"-1"
				};
			}else{
				data = {
						layer:layerId,
						action:"edit",
						madangky:feature.attributes["ma_dangky"],
						machebien:feature.attributes["ma_khaithac"],
						coor:sCoor,
						trangthai:feature.attributes["trangthai"]
					};
			}
			break;
		case LayerAssetName.thamDoKS:
			var sCoor = "";
			var sFirst = "";
			var sVertices = feature.geometry.getVertices();
			for(var j=0;j<sVertices.length;j++){
				var coor =sVertices[j].x+" "+sVertices[j].y;
				if(j==0)sFirst = coor;
				if(sCoor==""&&sCoor.length==0)sCoor = coor;
				else sCoor+=","+coor;
			}
			sCoor+=","+sFirst;
			if(type=="insert"){
				data = {
					layer:layerId,
					action:"insert",
					madangky:"",
					madean:"",
					coor:sCoor,
					trangthai:"-1"
				};
			}else{
				data = {
						layer:layerId,
						action:"edit",
						madangky:feature.attributes["ma_dangky"],
						madean:feature.attributes["ma_dean"],
						coor:sCoor,
						trangthai:feature.attributes["trangthai"]
					};
			}
			break;
		case LayerAssetName.diemThamDoNuoc:
			data = {};
			break;
		case LayerAssetName.diemKTNuocMat:
			data = {};
			break;
		case LayerAssetName.diemXaThai:
			data = {};
			break;
		case LayerAssetName.diemKhoanGieng:
			data = {};
			break;
		case LayerAssetName.diemNguonThai:
			data = {};
			break;
		case LayerAssetName.diemONhiem:
			data = {};
			break;
		case LayerAssetName.diemXuLy:
			data = {};
			break;
		case LayerAssetName.diemTapKet:
			data = {};
			break;
		default:
			break;
		}
		return data;
	}
};

function onChangeDiemCheBien(ctrl){
	var type = ctrl.value;
	var layer = $("#selectAssetInsertgeom").val();
	if(type=="insert"){
		if(layer==LayerAssetName.cheBienKhoangSan){
			resetDiemCheBien();
		}else if(layer==LayerAssetName.khaiThacKhoangSan){
			resetDiemKhaiThac();
		}else if(layer==LayerAssetName.thamDoKS){
			resetDiemThamDo();
		}
	}else if(type=="update"){
		var maDk = $("#txtMaDangKy_CheBien").val();
		if(maDk==""){
			$("#divDisplay_textError").html(LANGUAGE[""]);
		}
	}else if(type=="remove"){
		
	}
}

function resetDiemCheBien(){
	$("#txtCoorX_CheBien").val("");
	$("#txtCoorY_CheBien").val("");
	$("#txtMaDangKy_CheBien").val("");
	$("#txtMaCheBien").val("");
	$("#txtTrangThai_CheBien").val("0");
	$("#txtTypeAction_CheBien").val("insert");
	$("#divContentDiemCheBienGeom select").uniform();
}
function resetDiemKhaiThac(){
	var num = ctrl.value;
	for(var i=0;i<num;i++){
		var trCoor = document.getElementById("trCoor_khaithac_"+i);
		if(trCoor!=null)$(trCoor).remove();
	}
	$("#txtNumberVertices").val("0");
	$("#txtMaDangKy_KhaiThac").val("");
	$("#txtMaKhaiThac").val("");
	$("#txtTrangThai_KhaiThac").val("0");
	$("#txtTypeAction_KhaiThac").val("insert");
	$("#divContentVungKhaiThacGeom select").uniform();
}


function resetDiemThamDo(){
	var num = ctrl.value;
	for(var i=0;i<num;i++){
		var trCoor = document.getElementById("trCoor_thamdo_"+i);
		if(trCoor!=null)$(trCoor).remove();
	}
	$("#txtNumberVertices_ThamDo").val("1");
	$("#txtMaDangKy_ThamDo").val("");
	$("#txtMaDean").val("");
	$("#txtTrangThai_ThamDo").val("0");
	$("#txtTypeAction_ThamDo").val("insert");
	$("#divContentVungThamDoGeom select").uniform();
}

function getNumberVerticesThamDo(ctrl){
	var num = ctrl.value;
	for(var i=0;i<num;i++){
		var trCoor = document.getElementById("trCoor_thamdo_"+i);
		if(trCoor!=null)$(trCoor).remove();
	}
	arrInputCoor = [];
	var count = 0;
	var trSelect = document.getElementById("trSelectPolygon_ThamDo");
	for(var i=0;i<num;i++){
		var objTR = document.createElement("tr");
		objTR.id = "trCoor_thamdo_"+i;
		count++;
		var tdX = document.createElement("td");
		tdX.innerHTML = LANGUAGE["coorx"]+count;
		var tdInputX = document.createElement("td");
		var inputX = document.createElement("input");
		var divX = document.createElement("div");
		divX.setAttribute("class","input input-mdf wd-s1");
		inputX.type="text";
		inputX.id = "txtCoorX_ThamDo_"+i;
		inputX.onkeydown = txtKeyDownHoSo;
		divX.appendChild(inputX);
		tdInputX.appendChild(divX);
		arrInputCoor.push("#txtCoorX_ThamDo_"+i);
		var tdY = document.createElement("td");
		tdY.innerHTML = LANGUAGE["coory"]+count;
		var tdInputY = document.createElement("td");
		var inputY = document.createElement("input");
		var divY = document.createElement("div");
		divY.setAttribute("class","input input-mdf wd-s1");
		inputY.type="text";
		inputY.id = "txtCoorY_ThamDo_"+i;
		inputY.onkeydown = txtKeyDownHoSo;
		divY.appendChild(inputY);
		tdInputY.appendChild(divY);
		arrInputCoor.push("#txtCoorY_ThamDo_"+i);
		
		objTR.appendChild(tdX);
		objTR.appendChild(tdInputX);
		objTR.appendChild(tdY);
		objTR.appendChild(tdInputY);
		trSelect.parentNode.insertBefore(objTR, trSelect);
	}	
}